home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n5.arc / L5.C < prev    next >
Text File  |  1990-11-07  |  3KB  |  73 lines

  1. /* Program to search through an array spanning a linked list of
  2.    variable-sized blocks for all entries with a specified ID number
  3.    and return the average of the values of all such entries. Each of
  4.    the variable-sized blocks may contain any number of data entries,
  5.    stored in the form of two separate arrays, one for ID numbers and
  6.    one for values.
  7. */
  8.  
  9. #include <stdio.h>
  10. #ifdef __TURBOC__
  11. #include <alloc.h>
  12. #else
  13. #include <malloc.h>
  14. #endif
  15.  
  16. void main(void);
  17. void exit(int);
  18. extern unsigned int FindIDAverage2(unsigned int,struct BlockHeader *);
  19.  
  20. /* Structure that starts each variable-sized block */
  21. struct BlockHeader {
  22.    struct BlockHeader *NextBlock; /* pointer to next block, or NULL
  23.                                     if this is the last block in the
  24.                                     linked list */
  25.    unsigned int BlockCount;      /* the number of DataElement entries
  26.                                     in this variable-sized block */
  27. };
  28.  
  29. void main(void) {
  30.    int i,j;
  31.    unsigned int IDToFind;
  32.    struct BlockHeader *BaseArrayBlockPointer,*WorkingBlockPointer;
  33.    int *WorkingDataPointer;
  34.    struct BlockHeader **LastBlockPointer;
  35.  
  36.    printf("ID # for which to find average: ");
  37.    scanf("%d",&IDToFind);
  38.  
  39.    /* Build an array across 5 blocks, for testing */
  40.    /* Anchor the linked list to BaseArrayBlockPointer */
  41.    LastBlockPointer = &BaseArrayBlockPointer;
  42.    /* Create 5 blocks of varying sizes */
  43.    for (i = 1; i < 6; i++) {
  44.       /* Try to get memory for the next block */
  45.       if ((WorkingBlockPointer =
  46.             (struct BlockHeader *) malloc(sizeof(struct BlockHeader) +
  47.             sizeof(int) * 2 * i * 10)) == NULL) {
  48.          exit(1);
  49.       }
  50.       /* Set the # of data elements in this block */
  51.       WorkingBlockPointer->BlockCount = i * 10;
  52.       /* Link the new block into the chain */
  53.       *LastBlockPointer = WorkingBlockPointer;
  54.       /* Point to the first data field */
  55.       WorkingDataPointer = (int *) ((char *)WorkingBlockPointer +
  56.             sizeof(struct BlockHeader));
  57.       /* Fill in the data fields with ID numbers and values */
  58.       for (j = 0; j < (i * 10); j++, WorkingDataPointer++) {
  59.          *WorkingDataPointer = j;
  60.          *(WorkingDataPointer + i * 10) = i * 1000 + j;
  61.       }
  62.       /* Remember where to set the link from this block to the next */
  63.       LastBlockPointer = &WorkingBlockPointer->NextBlock;
  64.    }
  65.    /* Set the last block's "next block" pointer to NULL to indicate
  66.       that there are no more blocks */
  67.    WorkingBlockPointer->NextBlock = NULL;
  68.  
  69.    printf("Average of all elements with ID %d: %u\n",
  70.          IDToFind, FindIDAverage2(IDToFind, BaseArrayBlockPointer));
  71.    exit(0);
  72. }
  73.